package com.dream.utils.web.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspTagException;

import com.dream.utils.web.provider.AuthorityProvider;
import com.dream.utils.web.provider.SpringContextUtils;


/**
 * @copyright evan
 * @author evan
 * @Revision
 * @date 2012/09/22
 */
public class AuthorityWhenTag extends AuthorityTagSupport {

	private static final long serialVersionUID = 8941104468457894303L;
	
	//the when tag property
	private String authority;

	public void setAuthority(String authority) {
		this.authority = authority;
	}

	@Override
	protected boolean condition() throws JspTagException {
		
		AuthorityProvider authorityProvider = SpringContextUtils.getBean(pageContext.getServletContext(),"authorityProvider", AuthorityProvider.class);
		if(authorityProvider == null ){
			authorityProvider =  SpringContextUtils.getBean(pageContext.getServletContext(), AuthorityProvider.class);
		}
		boolean res = false;
		if(authorityProvider != null){
			String[] autorities = authority.split(",");
			res = authorityProvider.hasAuthority((HttpServletRequest)pageContext.getRequest(),autorities);
		}
		return res;
		
	}
}